﻿using Core.Identity.Roles.Dtos;
using Core.Identity.Roles.Features.Commands.AssignMenus;
using Core.Identity.Roles.Features.Commands.BatchDelete;
using Core.Identity.Roles.Features.Commands.Create;
using Core.Identity.Roles.Features.Commands.Enable;
using Core.Identity.Roles.Features.Commands.Update;
using Core.Identity.Roles.Features.Queries.GetAll;
using Core.Identity.Roles.Features.Queries.GetMenus;
using Core.Identity.Roles.Features.Queries.GetMenusById;
using Core.Identity.Roles.Features.Queries.WithPagination;
using Core.Models;
using Infrastructure.Auth.Policy;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;

namespace Infrastructure.Identity.Roles.Endpoints;

/// <summary>
/// 角色管理端点
/// </summary>
public static class RoleEndpoints
{
    /// <summary>
    /// 映射角色管理相关API端点
    /// </summary>
    public static IEndpointRouteBuilder MapRoleEndpoints(this IEndpointRouteBuilder app)
    {
        var group = app.MapGroup("/api/role")
            .WithTags("Role")
            .WithDescription("角色管理");

        // -----------------------
        // 分页查询角色
        // GET /api/role/page?page=1&size=10
        // -----------------------
        group.MapGet("/page", async (
            [AsParameters] RolesWithPaginationQuery request,
            [FromServices] IMediator mediator) =>
        {
            var data = await mediator.Send(request);
            return await Result<PaginatedData<RoleDto>>.SucceedAsync(data);
        })
        .RequirePermission("Role:View:Page")
        .WithDescription("分页查询角色")
        .WithMetadata(new EndpointNameMetadata("QueryRoles"));

        // -----------------------
        // 查询全部角色
        // GET /api/role/all
        // -----------------------
        group.MapGet("/all", async ([FromServices] IMediator mediator) =>
            await mediator.Send(new GetAllRolesQuery()))
            .RequirePermission("Role:View:All")
            .WithDescription("查询全部角色")
            .WithMetadata(new EndpointNameMetadata("GetAllRoles"));

        // -----------------------
        // 创建角色
        // POST /api/role
        // -----------------------
        group.MapPost("/", async (
            [FromBody] CreateRoleCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Role:Create")
            .WithDescription("创建角色")
            .WithMetadata(new EndpointNameMetadata(nameof(CreateRoleCommand)));

        // -----------------------
        // 修改角色
        // PUT /api/role/{id}
        // -----------------------
        group.MapPut("/{id:guid}", async (
            [FromBody] UpdateRoleCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Role:Update")
            .WithDescription("修改角色")
            .WithMetadata(new EndpointNameMetadata(nameof(UpdateRoleCommand)));

        // -----------------------
        // 批量删除角色
        // DELETE /api/role
        // -----------------------
        group.MapDelete("/", async (
            [FromBody] BatchDeleteRoleCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Role:Delete")
            .WithDescription("批量删除角色")
            .WithMetadata(new EndpointNameMetadata(nameof(BatchDeleteRoleCommand)));

        // -----------------------
        // 启用/禁用角色
        // POST /api/role/{id}/status
        // -----------------------
        group.MapPost("/{id:guid}/status", async (
            [FromBody] SetRoleEnabledStatusCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Role:Update:Status")
            .WithDescription("启用/禁用角色")
            .WithMetadata(new EndpointNameMetadata(nameof(SetRoleEnabledStatusCommand)));

        // -----------------------
        // 查询角色菜单（通用）
        // GET /api/role/menus
        // -----------------------
        group.MapGet("/menus", async ([FromServices] IMediator mediator) =>
            await mediator.Send(new GetRoleMenusQuery()))
            .RequirePermission("Role:Menu:View")
            .WithDescription("查询角色菜单")
            .WithMetadata(new EndpointNameMetadata("GetRoleMenus"));

        // -----------------------
        // 根据角色ID查询菜单权限
        // POST /api/role/{id}/menus
        // -----------------------
        group.MapPost("/{id:guid}/menus", async (
            [FromBody] GetRoleMenusByIdQuery request,
            [FromServices]     IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Role:Menu:View")
            .WithDescription("查询指定角色的菜单权限")
            .WithMetadata(new EndpointNameMetadata("GetRoleMenusById"));

        // -----------------------
        // 分配菜单到角色
        // POST /api/role/{id}/assign
        // -----------------------
        group.MapPost("/{id:guid}/assign", async (
            [FromBody] AssignMenusToRoleCommand request,
            [FromServices] IMediator mediator) =>
            await mediator.Send(request))
            .RequirePermission("Role:Menu:Assign")
            .WithDescription("分配菜单")
            .WithMetadata(new EndpointNameMetadata(nameof(AssignMenusToRoleCommand)));

        return app;
    }
}
